package scr 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import ru.gosoplata.model.TmpModel;
	import ru.gosoplata.view.FooterPayBackText;
	import ru.gosoplata.view.FooterView;
	import uicontrols.ButtonBase;
	/**
	 * ...
	 * @author a.orleansky
	 */
	public class ScreenIndexServices extends ScreenBase implements IScreen
	{
		private var btn_up_left:ButtonBase;
		private var btn_up_right:ButtonBase;
		private var btn_down_left:ButtonBase;
		private var btn_down_right:ButtonBase;
		
		private var btn_up_left_iconurl:String = TmpModel.IMG_FOLDER+"/oplata_uslug/images/sotovaya_svyaz.png";
		private var btn_up_right_iconurl:String = TmpModel.IMG_FOLDER+"/oplata_uslug/images/drugie_uslugi.png";
		private var btn_down_left_iconurl:String = TmpModel.IMG_FOLDER+"/oplata_uslug/images/kom_tel.png";
		private var btn_down_right_iconurl:String = TmpModel.IMG_FOLDER + "/oplata_uslug/images/internet_ip_tel.png";
		
		private var pic_count:Number=0;
		
		private var _footer:FooterView=new FooterView();
		private var _footerPayBackText:FooterPayBackText = new FooterPayBackText();
		private var _center_btn_contayner:Sprite=new Sprite();
		
		public function ScreenIndexServices() 
		{
			super("index_services");
		}
		
		override protected function init(e:Event):void 
		{
			super.init(e);
			
			addChild(_footer);
			addChild(_footerPayBackText);
			
			addChild(_center_btn_contayner)
			btn_up_left = new ButtonBase("servise_up_left", 0x00FF00,btn_up_left_iconurl);
			btn_up_left.addEventListener(ButtonBase.ON_PRESS_UI_BUTTON, pressBtn);
			btn_up_left.addEventListener(ButtonBase.ON_PIC_LOADED, allPicLoaded);
			_center_btn_contayner.addChild(btn_up_left);
			pic_count++;
			
			btn_down_left = new ButtonBase("servise_down_left", 0x00FF00,btn_down_left_iconurl);
			btn_down_left.addEventListener(ButtonBase.ON_PRESS_UI_BUTTON, pressBtn);
			btn_down_left.addEventListener(ButtonBase.ON_PIC_LOADED, allPicLoaded);
			_center_btn_contayner.addChild(btn_down_left);
			pic_count++;
			
			btn_up_right = new ButtonBase("servise_up_right", 0x00FF00,btn_up_right_iconurl);
			btn_up_right.addEventListener(ButtonBase.ON_PRESS_UI_BUTTON, pressBtn);
			btn_up_right.addEventListener(ButtonBase.ON_PIC_LOADED, allPicLoaded);
			_center_btn_contayner.addChild(btn_up_right);
			pic_count++;
			
			btn_down_right = new ButtonBase("servise_down_right", 0x00FF00,btn_down_right_iconurl);
			btn_down_right.addEventListener(ButtonBase.ON_PRESS_UI_BUTTON, pressBtn);
			btn_down_right.addEventListener(ButtonBase.ON_PIC_LOADED, allPicLoaded);
			_center_btn_contayner.addChild(btn_down_right);
			pic_count++;
		}
		
		override public function reDraw():void
		{
			TmpModel.payment.clearExtraData();	//TODO: ScreenIndexServices reDraw clearExtraData - опять вью решает что делать!!!!		
			
			_center_btn_contayner.x = (1280 - _center_btn_contayner.width) / 2;
			_center_btn_contayner.y = (1024 - _center_btn_contayner.height) / 2;
			
			loadBanner(TmpModel.IMG_FOLDER+"/sotovie_operatori/images/reklama_title.png");
			_footerPayBackText.visible = false;
			if (TmpModel.payment.operation && TmpModel.payment.operation.length > 5)
			{
				_center_btn_contayner.y += 40; 
				loadBanner(TmpModel.IMG_FOLDER+"/service/banner.png");
				_footerPayBackText.update(TmpModel.payment.payBack);
				_footerPayBackText.visible = true;
			}
			
			_footer.reDraw();
		}
		
		private function allPicLoaded(e:Event):void 
		{
			pic_count--;
			if (pic_count == 0) 
			{
				
				//btn_up_left.x = stage.stageWidth/2 - (btn_up_left.width+20);
				//btn_up_left.y = stage.stageHeight / 2 - (btn_up_left.height + 20);
				//
				//btn_up_right.x = stage.stageWidth / 2 +20;
				//btn_up_right.y = btn_up_left.y;
				//
				//btn_down_left.x = btn_up_left.x;
				//btn_down_left.y = stage.stageHeight / 2 +20;
				//
				//btn_down_right.x = btn_up_right.x;
				//btn_down_right.y = btn_down_left.y;
				
				btn_up_right.x = btn_down_right.x = btn_up_left.width + 20;
				btn_down_left.y = btn_down_right.y = btn_up_left.height + 20;
				reDraw();
			}
		}
		
		private function pressBtn(e:Event):void {
			switch(e.target.name) 
			{
				case "servise_up_left"://sotovaya_svyaz
					TmpModel.tmpGlobalController.controllerSelectProvider("cellular");
					break;
				case "servise_down_left"://kom_tel
					TmpModel.tmpGlobalController.controllerSelectProvider("tv");
					break;
				case "servise_up_right"://drugie_uslugi
					TmpModel.tmpGlobalController.controllerSelectProvider("other");
					break;
				case "servise_down_right"://internet_ip_tel
					TmpModel.tmpGlobalController.controllerSelectProvider("internet");
					break;
			}
		}
	}
}